﻿using System;
using System.Device.Location;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using AuchanScan.Datas;
using AuchanScan.ViewModels;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps;

namespace AuchanScan.Views
{
    public partial class MapPage : PhoneApplicationPage
    {
        private Control exPin;

        public MapPage()
        {
            InitializeComponent();

            DataContext = ViewModelLocator.GeoViewModelStatic;

            MyMap.Center = ViewModelLocator.GeoViewModelStatic.CenterMap;

            GeoLocMe();
            //ApplicationBar.IsVisible = true;
            autour.SelectionChanged += autour_SelectionChanged;

            Loaded += MapPage_Loaded;
        }

        private void GeoLocMe()
        {
            var geo = new GeoWatcher();

            ViewModelLocator.GeoViewModelStatic.SetLoading(true);

            geo.CoordinateChanged += (senderr, er) => {
                    Deployment.Current.Dispatcher.BeginInvoke(() => { MyMap.Center = er.Position.Location; });
                    ViewModelLocator.GeoViewModelStatic.ComputeCoordinate(er.Position.Location);
            };

            geo.LaunchGPS();
        }

        private void MapPage_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var fe = sender as FrameworkElement;
            var pin = VisualTreeHelper.GetParent(VisualTreeHelper.GetParent(fe)) as Control;
            if (pin == exPin)
            {
                /*    var link = (pin.DataContext as ViewModels.Place).Link;
                    link = link.Replace("wikipedia.", "m.wikipedia.");
                    /*Microsoft.Phone.Tasks.WebBrowserTask task = new Microsoft.Phone.Tasks.WebBrowserTask();

                
                   task.URL = link;
                    task.Show();
                    NavigationService.Navigate(new Uri("/BrowsePage.xaml?uri="+HttpUtility.UrlEncode( link),UriKind.Relative));
                */
                VisualStateManager.GoToState(pin, "Base", true);
                exPin = null;
                return;
            }
            Canvas.SetZIndex(pin, 999);
            VisualStateManager.GoToState(pin, "Selected", true);
            try
            {
                GeoCoordinate pos = (pin as Pushpin).Location;
                var newpos = new GeoCoordinate
                    {
                        Longitude = pos.Longitude + 0.1/MyMap.ZoomLevel,
                        Latitude = pos.Latitude,
                    };
                MyMap.Center = newpos;
            }
            catch
            {
            }
            if (exPin != null)
            {
                VisualStateManager.GoToState(exPin, "Base", true);
            }

            DependencyObject reff = VisualTreeHelper.GetParent(pin);
            foreach (object itr in MapStations.Items)
            {
                DependencyObject it = MapStations.ItemContainerGenerator.ContainerFromItem(itr);
                if (it == reff)
                    Canvas.SetZIndex((FrameworkElement) it, 0999);
                else
                    Canvas.SetZIndex((FrameworkElement) it, 0);
            }
            exPin = pin;
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 0)
                return;

            //  var place = e.AddedItems[0] as ViewModels.Place;
            //NavigationService.Navigate(new Uri("/BrowsePage.xaml?uri=" + HttpUtility.UrlEncode(link), UriKind.Relative));

            ((ListBox) sender).SelectedItem = null;
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            var place = ((FrameworkElement) sender).DataContext as Magasin;
            //  var link = place.Link.Replace("wikipedia.", "m.wikipedia.");
            //  NavigationService.Navigate(new Uri("/BrowsePage.xaml?uri=" + HttpUtility.UrlEncode(link), UriKind.Relative));
        }

        private void autour_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        }

        private void CenterApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            GeoLocMe();
        }

        private void AroundMeApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            ViewModelLocator.GeoViewModelStatic.ComputeCoordinate(MyMap.Center);
        }
    }
}
